﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;

namespace CodeSmackdown.Web.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Title"] = "code smackdown";
            //ViewData["Message"] = "Choose A Winner";

            Models.CodeSmackdownDataContext ctx = new Models.CodeSmackdownDataContext();

            var snippet = ctx.LoadRandomSnippet();
            Session.SetSnippet(snippet);
            return View("Index", new Models.SnippetModel(snippet, Session.User()));
        }

        public ActionResult ViewSnippet(int snippetID)
        {
            ViewData["Title"] = "code smackdown";
            //ViewData["Message"] = "Choose A Winner";

            Models.CodeSmackdownDataContext ctx = new Models.CodeSmackdownDataContext();

            var snippet = ctx.GetSnippet(snippetID);
            Session.SetSnippet(snippet);
            return View("Index", new Models.SnippetModel(snippet, Session.User()));
        }

        public ActionResult About()
        {
            ViewData["Title"] = "about 'code smackdown'";

            return View();
        }

        [Authorize]
        [ValidateInput(false)]
        public ActionResult CreateComment(string comment)
        {
            ViewData["Title"] = "code smackdown";
            //ViewData["Message"] = "Choose A Winner";

            if (string.IsNullOrEmpty(comment))
            {
                ModelState.AddModelError("comment", "No comment body has been specified");
            }

            if (ModelState.IsValid)
            {
                var ctx = new CodeSmackdown.Web.Models.CodeSmackdownDataContext();
                Session.SetSnippet(ctx.CreateComment(Session.Snippet(), comment, Session.User()));
            }

            return View("Index", new Models.SnippetModel(Session.Snippet(), Session.User()));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult VoteSnippet1()
        {
            return Vote(true);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult VoteSnippet2()
        {
            return Vote(false);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult VoteNeither()
        {
            return Vote(null);
        }

        private ActionResult Vote(bool? voteSnippet1)
        {
            ViewData["Title"] = "code smackdown";
            //ViewData["Message"] = "Thanks for voting!";

            var ctx = new CodeSmackdown.Web.Models.CodeSmackdownDataContext();
            var savedSnippet = ctx.SubmitVote(Session.User(), Session.Snippet(), voteSnippet1);
            Session.SetSnippet(savedSnippet);
            
            return View("Index", new Models.SnippetModel(savedSnippet, Session.User()));
        }
    }
}
